import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:get/get_state_manager/src/simple/get_controllers.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_multiple_bottom_sheet.dart';
import 'package:safetymonitor/app/components/nesting_bottom_close_swipe_down.dart';
import 'package:safetymonitor/app/core/ext/widget_extension.dart';
import 'package:safetymonitor/app/core/utils/user_manager.dart';
import 'package:safetymonitor/app/data/entities/application/corporation_name_entity.dart';
import 'package:safetymonitor/modules/home/enterprise/company_info/company_info_controller.dart';
import 'package:safetymonitor/modules/home/enterprise/emergency_plan/emergency_plan_controller.dart';
import 'package:safetymonitor/modules/home/home_page/home_page_control.dart';

import 'people_or_papers/people_or_papers_controller.dart';

class EnterpriseController extends GetxController {
  ScrollController controller = ScrollController();
  final GlobalKey<NestingBottomCloseOnSwipeDownWidgetState> globalKey =
      GlobalKey();

  // 选中的公司
  CorporationNameEntity? _selectId;

  // 公司列表
  List<CorporationNameEntity> nameList = [];

  ///获取账号的当前公司名字
  String? userCompanyName;

  ///选择的公司
  String selectCompany = '';

  /// 选中的公司id
  int? get selectId => _selectId?.id;

  ///主公司 写死（默认）
  String currentMainCompany = '金钼集团';

  final List<String> title = [
    '企业基本信息',
    '人员及证件照',
    '应急预案',
  ];

  ///主公司和分公司
  // final List<String> companyList = [
  // '请选择',
  // '金钼汝阳',
  // '矿山分公司',
  // '金属分公司',
  // '冶炼分公司',
  // '化学分公司',
  // '五洲矿业',
  // ];

  @override
  void onInit() {
    super.onInit();
    userCompanyName = UserManager.getUserCompanyName() ?? '';
    //浅拷贝
    nameList = List.from(Get.find<HomePageControl>().nameList);
    for (int i = 0; i < nameList.length; i++) {
      if (nameList[i].name == currentMainCompany) {
        nameList.remove(nameList[i]);
      }
    }
    if (currentMainCompany == userCompanyName && nameList.isNotEmpty) {
      //只有是主公司才才取第一个 并获取数据
      _selectId = nameList.first;
      selectCompany = getString();
    }
    // _getDepartmentList();
  }

  /// 获取部门列表
  // void _getDepartmentList() async {
  //   final res = await HttpUtil().generateGetRequest<List<CorporationNameEntity>>(Interface.deptList, queryParameters: {
  //     'flag': 2,
  //   }).execute();
  //   res.onSuccess((result) {
  //     if (result.data != null) {
  //       _selectId = result.data!.first;
  //       selectCompany = getString();
  //       _nameList = result.data!;
  //     }
  //   });
  // }

  String getString() {
    if (_selectId != null) {
      return _selectId!.name!.trim();
    }
    return '公司名';
  }

  // 选取公司
  void selectCompanyOn() {
    CustomEquipmentBottomSheet<CorporationNameEntity>(
      dataList: nameList,
      checkedList: _selectId != null ? [_selectId!] : [],
      onConfirm: (value) {
        if (value.isNotEmpty) {
          _selectId = value[0];
          selectCompany = getString();
          Get.find<CompanyInfoController>().information();
          Get.find<EmergencyPlanController>()
              .globalKey
              .currentState
              ?.onRefresh();
          Get.find<PeopleOrPapersController>()
              .globalKey
              .currentState
              ?.onRefresh();
          update();
        }
      },
      title: '选择公司',
      isMultiple: false,
    ).showBottomSheetDialog();
  }
}
